import fs from 'fs';
import path from 'path';

export function cleanDirs(dirName) {
  const rootDir = process.cwd();
  const isTargetDir = (dir) => dir === dirName;

  function scanDirectory(currentPath) {
    fs.readdirSync(currentPath, { withFileTypes: true }).forEach((dirent) => {
      const fullPath = path.join(currentPath, dirent.name);
      if (dirent.isDirectory()) {
        if (isTargetDir(dirent.name)) {
          console.log(`Removing: ${fullPath}`);
          fs.rmSync(fullPath, { recursive: true, force: true });
        } else scanDirectory(fullPath);
      }
    });
  }

  scanDirectory(rootDir);
}

export function cleanAll() {
  cleanDirs('node_modules');
  cleanDirs('dist');
}

const [, , command] = process.argv;
if (command == 'all') {
  cleanAll();
} else if (command === 'node_modules') {
  cleanDirs('node_modules');
} else if (command === 'dist') {
  cleanDirs('dist');
}
